package vek;

import java.util.ArrayList;
import java.util.Timer;
import vek.GUI.GUIMain;
import vek.ObjectCreation.ObjectCreator;
import vek.ObjectDatabase.DatabaseConnector;
import vek.ObjectDatabase.DatabaseConnector.DatabaseType;
import vek.ObjectDatabase.POIDatabase;
import vek.ObjectDatabase.SimpleDatabase;
import vek.Objects.POI;
//The import of vek.Update.Updater may not be used in the current version, but it is to ease testing.
import vek.Update.UpdateTimer;
import vek.Update.Updater;
import vek.XML.Parsers.XMLParserMain;
import vek.XML.parsedObjects.ParsedObjectInterface;

/**
 * This class initiates the rest of the program.
 * First it gets the ParsedObjects by calling the ParserMain.
 * Then it creates the POIs using ObjectCreator.
 * Then it loads the POIs into the SimpleDatabase.
 * Then it creates the Database Connector and loads the SimpleDatabase into it.
 * Then it starts the GUI
 * <p>
 * NOTE:
 * This class uses ArrayLists which contain ArrayLists which then contain objects
 * This is effectively a 2D Array, but a dynamic one are used because the amount of XML files are unknwon
 * 
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class Main2 {

    /**
     * 
     * @param args the command line arguments
     */
    
    private static DatabaseConnector databaseConnector;
    
    /**
     * main method which starts the program.
     * 
     * This method first calls the parsers, receives the parsedobjects, then puts them into the simple database.
     * then it starts the GUI called GUIMain
     * 
     * @param args not used
     */
    
    public static void main(String[] args) {
        
        XMLParserMain.parseSettings("Data/settings.xml");
        UpdateTimer updateTimer = UpdateTimer.getUpdateTimer();
        
        
        ArrayList<ArrayList<ParsedObjectInterface>> parsedObjects = parseXMLFilesAndReturnParsedObjects("Data/latestupdate");
        ArrayList<POI> allPOIObjects = createPOIObjects(parsedObjects);
        POIDatabase database = new SimpleDatabase(DatabaseType.POI, false, 10, allPOIObjects) ;
        databaseConnector = new DatabaseConnector();
        databaseConnector.addDatabase(database);
        
         updateTimer.scheduleAtFixedRate(new Updater(databaseConnector), TerminalSettings.getUpdateInteval()  * (1000 * 60), TerminalSettings.getUpdateInteval()  * (1000 * 60));
        
        new GUIMain(databaseConnector);
        
    }
    
    /**
     * Method to get the DatabaseConnector.
     * This method is solely used for debugging purposes, specifically to facilitate access
     * 
     * @return the Database connector used
     */
    
    public static DatabaseConnector getDBC()
    {
        assert databaseConnector != null : "Main2: getDBC: DataBaseConnector not instantiateted";
        return databaseConnector;
    }
    
    /**
     * Parses the XML Files and returns the parsedObjects created by the parsers.
     * 
     * It first parses all the xml files in the directory given by the string.
     * Then it returns the parsedObjects which are created by the parsers.
     * <p>
     * NOTE: the objects returned are polymorphed and contained in a single ArrayList, 
     * which then contains ArrayLists which contain ParsedObjectInterface objects
     * 
     * @param s the directory where the XML files to be parsed reside
     * @return an ArrayList of ArrayLists containing ParsedObjectInterface objects (i.e. a dynamic 2D array based on the ArrayList class)
     */
    
    public static ArrayList<ArrayList<ParsedObjectInterface>> parseXMLFilesAndReturnParsedObjects(String s)
    {
        ArrayList<ArrayList<ParsedObjectInterface>> parsedObjects = XMLParserMain.parseAllXMLFilesInDirectory(s);
        assert parsedObjects != null && parsedObjects.size() != 0 : "Main2: parseXMLFilesAndReturnedParsedObjects: parsedObjects == null or has no objects";
        return parsedObjects;
    }
    
    /**
     * creates POI ojbects based on ParsedObjectInterface objects.
     * 
     * This method creates POI ojbects using the ObjectCreator and returns a single ArrayList of POI objects
     * 
     * @param parsedObjects ArrayList containing ArrayLists which contain parsedObjects (i.e. a dynamic 2D array based on the ArrayList class)
     * @return an ArrayList which contains POI objects
     */
    
    public static ArrayList<POI> createPOIObjects(ArrayList<ArrayList<ParsedObjectInterface>> parsedObjects)
    {
        ObjectCreator creator = new ObjectCreator(parsedObjects);
        return creator.createAllPOIObjects(parsedObjects);
    }
  

}

